﻿using System;
using System.Data;
using System.Data.SqlClient;
using APP.HELPER.Data.Helper;
using APP.HELPER.Utils.CustomizeToolbox;
using DevExpress.XtraEditors;
using ComboBox = System.Windows.Forms.ComboBox;

namespace Dictionary.ERP
{
    public class DIC_RATE
    {
        private bool m_Active;
        private double m_Coefficient;
        private string m_Description;
        private string m_GroupRateCode;
        private string m_RateCode;
        private string m_RateName;

        public DIC_RATE()
        {
            m_RateCode = "";
            m_GroupRateCode = "";
            m_RateName = "";
            m_Coefficient = 0.0;
            m_Description = "";
            m_Active = true;
        }

        public DIC_RATE(string RateCode, string GroupRateCode, string RateName, double Coefficient, string Description,
            bool Active)
        {
            m_RateCode = RateCode;
            m_GroupRateCode = GroupRateCode;
            m_RateName = RateName;
            m_Coefficient = Coefficient;
            m_Description = Description;
            m_Active = Active;
        }

        public bool Active
        {
            get { return m_Active; }
            set { m_Active = value; }
        }

        public double Coefficient
        {
            get { return m_Coefficient; }
            set { m_Coefficient = value; }
        }

        public string Description
        {
            get { return m_Description; }
            set { m_Description = value; }
        }

        public string GroupRateCode
        {
            get { return m_GroupRateCode; }
            set { m_GroupRateCode = value; }
        }

        public string ProductName
        {
            get { return "Class DIC_RATE"; }
        }

        public string ProductVersion
        {
            get { return "1.0.0.0"; }
        }

        public string RateCode
        {
            get { return m_RateCode; }
            set { m_RateCode = value; }
        }

        public string RateName
        {
            get { return m_RateName; }
            set { m_RateName = value; }
        }

        public void AddCombo(ComboBox combo)
        {
            GridCalendarControlLib.TableToComboBox(combo, GetList(), "RateName", "RateCode");
        }

        public void AddComboAll(ComboBox combo)
        {
            var list = new DataTable();
            list = GetList();
            DataRow row = list.NewRow();
            row["RateCode"] = "All";
            row["RateName"] = "Tất cả";
            list.Rows.InsertAt(row, 0);
            GridCalendarControlLib.TableToComboBox(combo, list, "RateName", "RateCode");
        }

        public void AddComboEdit(ComboBoxEdit combo)
        {
            var list = new DataTable();
            list = GetList();
            foreach (DataRow row in list.Rows)
            {
                combo.Properties.Items.Add(row["RateName"]);
            }
        }

        public string Delete()
        {
            string[] myParams = {"@RateCode"};
            object[] myValues = {m_RateCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_RATE_Delete", myParams, myValues);
        }

        public string Delete(string RateCode)
        {
            string[] myParams = {"@RateCode"};
            object[] myValues = {RateCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_RATE_Delete", myParams, myValues);
        }

        public string Delete(SqlConnection myConnection, string RateCode)
        {
            string[] myParams = {"@RateCode"};
            object[] myValues = {RateCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_RATE_Delete", myParams, myValues);
        }

        public string Delete(SqlTransaction myTransaction, string RateCode)
        {
            string[] myParams = {"@RateCode"};
            object[] myValues = {RateCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_RATE_Delete", myParams, myValues);
        }

        public bool Exist(string RateCode)
        {
            bool hasRows = false;
            string[] myParams = {"@RateCode"};
            object[] myValues = {RateCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("DIC_RATE_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                helper.Close();
                reader = null;
            }
            return hasRows;
        }

        public string Get(string RateCode)
        {
            string str = "";
            string[] myParams = {"@RateCode"};
            object[] myValues = {RateCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("DIC_RATE_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_RateCode = Convert.ToString(reader["RateCode"]);
                    m_GroupRateCode = Convert.ToString(reader["GroupRateCode"]);
                    m_RateName = Convert.ToString(reader["RateName"]);
                    m_Coefficient = Convert.ToDouble(reader["Coefficient"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    m_Active = Convert.ToBoolean(reader["Active"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlConnection myConnection, string RateCode)
        {
            string str = "";
            string[] myParams = {"@RateCode"};
            object[] myValues = {RateCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myConnection, "DIC_RATE_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_RateCode = Convert.ToString(reader["RateCode"]);
                    m_GroupRateCode = Convert.ToString(reader["GroupRateCode"]);
                    m_RateName = Convert.ToString(reader["RateName"]);
                    m_Coefficient = Convert.ToDouble(reader["Coefficient"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    m_Active = Convert.ToBoolean(reader["Active"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlTransaction myTransaction, string RateCode)
        {
            string str = "";
            string[] myParams = {"@RateCode"};
            object[] myValues = {RateCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myTransaction, "DIC_RATE_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_RateCode = Convert.ToString(reader["RateCode"]);
                    m_GroupRateCode = Convert.ToString(reader["GroupRateCode"]);
                    m_RateName = Convert.ToString(reader["RateName"]);
                    m_Coefficient = Convert.ToDouble(reader["Coefficient"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    m_Active = Convert.ToBoolean(reader["Active"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public DataTable GetList()
        {
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("DIC_RATE_GetList");
        }

        public DataTable GetListByCode(string GroupRateCode)
        {
            string[] myParams = {"@GroupRateCode"};
            object[] myValues = {GroupRateCode};
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("DIC_RATE_GetListByCode", myParams, myValues);
        }

        public string Insert()
        {
            string[] myParams = {"@RateCode", "@GroupRateCode", "@RateName", "@Coefficient", "@Description", "@Active"};
            object[] myValues = {m_RateCode, m_GroupRateCode, m_RateName, m_Coefficient, m_Description, m_Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_RATE_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction)
        {
            string[] myParams = {"@RateCode", "@GroupRateCode", "@RateName", "@Coefficient", "@Description", "@Active"};
            object[] myValues = {m_RateCode, m_GroupRateCode, m_RateName, m_Coefficient, m_Description, m_Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_RATE_Insert", myParams, myValues);
        }

        public string Insert(string RateCode, string GroupRateCode, string RateName, double Coefficient,
            string Description, bool Active)
        {
            string[] myParams = {"@RateCode", "@GroupRateCode", "@RateName", "@Coefficient", "@Description", "@Active"};
            object[] myValues = {RateCode, GroupRateCode, RateName, Coefficient, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_RATE_Insert", myParams, myValues);
        }

        public string Insert(SqlConnection myConnection, string RateCode, string GroupRateCode, string RateName,
            double Coefficient, string Description, bool Active)
        {
            string[] myParams = {"@RateCode", "@GroupRateCode", "@RateName", "@Coefficient", "@Description", "@Active"};
            object[] myValues = {RateCode, GroupRateCode, RateName, Coefficient, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_RATE_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction, string RateCode, string GroupRateCode, string RateName,
            double Coefficient, string Description, bool Active)
        {
            string[] myParams = {"@RateCode", "@GroupRateCode", "@RateName", "@Coefficient", "@Description", "@Active"};
            object[] myValues = {RateCode, GroupRateCode, RateName, Coefficient, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_RATE_Insert", myParams, myValues);
        }

        public string NewID()
        {
            return SqlHelper.GenCode("DIC_RATE", "RateCode", "DG");
        }

        public string Update()
        {
            string[] myParams = {"@RateCode", "@GroupRateCode", "@RateName", "@Coefficient", "@Description", "@Active"};
            object[] myValues = {m_RateCode, m_GroupRateCode, m_RateName, m_Coefficient, m_Description, m_Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_RATE_Update", myParams, myValues);
        }

        public string Update(string RateCode, string GroupRateCode, string RateName, double Coefficient,
            string Description, bool Active)
        {
            string[] myParams = {"@RateCode", "@GroupRateCode", "@RateName", "@Coefficient", "@Description", "@Active"};
            object[] myValues = {RateCode, GroupRateCode, RateName, Coefficient, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_RATE_Update", myParams, myValues);
        }

        public string Update(SqlConnection myConnection, string RateCode, string GroupRateCode, string RateName,
            double Coefficient, string Description, bool Active)
        {
            string[] myParams = {"@RateCode", "@GroupRateCode", "@RateName", "@Coefficient", "@Description", "@Active"};
            object[] myValues = {RateCode, GroupRateCode, RateName, Coefficient, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_RATE_Update", myParams, myValues);
        }

        public string Update(SqlTransaction myTransaction, string RateCode, string GroupRateCode, string RateName,
            double Coefficient, string Description, bool Active)
        {
            string[] myParams = {"@RateCode", "@GroupRateCode", "@RateName", "@Coefficient", "@Description", "@Active"};
            object[] myValues = {RateCode, GroupRateCode, RateName, Coefficient, Description, Active};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_RATE_Update", myParams, myValues);
        }
    }
}